package com.family.system.handler;

import com.family.common.result.Result;
import com.family.system.exception.GuiguException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName GlobalExceptionHandler
 * @Date 2023-05-05 17:36
 * @AUTHOR family
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理器
     */
    @ExceptionHandler(Exception.class)
    public Result<Object> error(Exception e) {
        e.printStackTrace();
        return Result.fail();
    }

    /**
     * 执行特定异常
     */
    @ExceptionHandler(ArithmeticException.class)
    public Result<Object> error(ArithmeticException e) {
        e.printStackTrace();
        return Result.fail().message("执行了特定异常处理器");
    }

    /**
     * 添加自定义异常
     */
    @ExceptionHandler(GuiguException.class)
    public Result<Object> error(GuiguException e) {
        e.printStackTrace();
        return Result.fail().message(e.getMessage()).code(e.getCode());
    }

}
